﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Python
{
    class Controller
    {
        public static int _FieldHeght = 56;
        public static int _FieldWidth = 79;
        public static int _xSnakeStartPosition = 25;
        public static int _ySnakeStartPosition = 12;
        public static String Title = "   Python game";



        static void Main(string[] args)
        {
            Console.Title = Title;
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.SetWindowSize(_FieldWidth, _FieldHeght);
            Field GameField = new Field(0, 0, _FieldWidth, _FieldHeght);
            Controller ControllerIns = new Controller();
            Point rabbitPosition;
            Snake Python = new Snake();
            Rabbit rabbit1 = new Rabbit(Python.GetHeadSnakePoint);
            Python.IsRabbitCauht = false;
            rabbitPosition = rabbit1.RabbitPosition;
            if (rabbitPosition.X == Python.GetHeadSnakePoint.X && rabbitPosition.Y == Python.GetHeadSnakePoint.Y)
            {
                Console.Beep();
            }

            DateTime Tm = DateTime.Now;
            var Delay = new TimeSpan();

            ConsoleKeyInfo key;

            for (; ; )
            {
                if (Console.KeyAvailable)
                {
                    key = Console.ReadKey(true);
                    if (key.Key.ToString() == "DownArrow")
                    {
                        Python.WantGoDown();
                    }
                    else if (key.Key.ToString() == "UpArrow")
                    {
                        Python.WantGoUp();
                    }
                    else if (key.Key.ToString() == "LeftArrow")
                    {
                        Python.WantGoLeft();
                    }
                    else if (key.Key.ToString() == "RightArrow")
                    {
                        Python.WantGoRight();
                    }
                    else if (key.Key == ConsoleKey.Escape)
                        return;
                }
                rabbitPosition = CheckIsRabbitCaught(rabbitPosition, Python);
                Delay = DateTime.Now - Tm;
                if (Delay.TotalMilliseconds > 150.0)
                {
                    Python.ContinueMovementSnake();  //   <---------------------------------<<<<<
                    Tm = DateTime.Now;
                    rabbitPosition = CheckIsRabbitCaught(rabbitPosition, Python);
                }
            }
        }

        private static Point CheckIsRabbitCaught(Point rabbitPosition, Snake Python)
        {
            if (rabbitPosition.X == Python.GetHeadSnakePoint.X && rabbitPosition.Y == Python.GetHeadSnakePoint.Y)
            {
                Console.Beep();
                Python.IsRabbitCauht = true;
                Python.RecalculateNewBody();
                Rabbit rabbit = new Rabbit(Python.GetHeadSnakePoint);   // Create a new rabbit
                rabbitPosition = rabbit.RabbitPosition;
                Python.IsRabbitCauht = false;

            }
            return rabbitPosition;   // Position of new rabbit
        }
    }
}
